package com.party.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.party.domain.Blurb;

import com.party.domain.Img;
import com.party.domain.Product;


@Mapper
public interface ProductMapper 
{
	//增
	
	//添加商品
	@Insert("INSERT INTO Product(pid,product_type,product_add_time,product_valid_time,product_city,product_description,product_title,product_value) VALUES(#{pid},#{type},#{addTime},#{validTime},#{city},#{description},#{title},#{value})")
	@Options(useGeneratedKeys=true, keyProperty="pid")
	@SelectKey(before=false,keyProperty="pid",resultType=Integer.class, statement = { "SELECT LAST_INSERT_ID()" })
	void inster(Product product);
	//添加商品详情
	@Insert("INSERT INTO Blurb(bid,product_id,blurb_address,blurb_number_people,blurb_number_bed,blurb_number_room,blurb_supporting_facility,blurb_label,blurb_supplement,blurb_warm_prompt) VALUES(#{bid},#{productId},#{address},#{numberPeople},#{numberBed},#{numberRoom},#{supportingFacility},#{blurbLabel},#{supplement},#{warmPrompt})")
	void insterBlurb(Blurb blurb);
	//添加图片
	@Insert("INSERT INTO Img(iid,product_id,img_address) VALUES(#{iid},#{productid},#{address})")
	void insterImg(Img img);
	
	
	
	//查

	//根据类型查询商品，分页
	@Select("SELECT * FROM product p LEFT JOIN img i ON p.pid=i.product_id WHERE p.product_type=#{type} order by p.pid desc LIMIT #{start},#{size}")
	@ResultMap("com.party.mapper.ProductMapper.ProductBaseMapped")
	List<Product> selectTypeProduct(@Param("type")String type,@Param("start")int start,@Param("size")int size);
	
	//根据二级类型查询商品，分页
	@Select("SELECT * FROM blurb b,product p LEFT JOIN img i ON p.pid=i.product_id WHERE p.pid=b.product_id AND b.blurb_type=#{type} order by p.pid desc LIMIT #{start},#{size}")
	@ResultMap("com.party.mapper.ProductMapper.ProductBaseMapped")
	List<Product> selectBlurbTypeProduct(@Param("type")String type,@Param("start")int start,@Param("size")int size);
	
	//试验语句
	@Select("SELECT * FROM Product WHERE product_type =#{type}")
	List<Product> selectType(@Param("type")String type);
	
	//id查询商品
	@Select("SELECT * FROM Product WHERE pid = #{id}")
	@ResultMap("com.party.mapper.ProductMapper.ProductBaseMapped")
	Product selectIdProduct(int id);
	
	//用id查询商品集合对象
	//List<Object> findById(int pid);

	
	//用商品id查询商品详情
	@Select("SELECT * FROM Blurb WHERE product_id = #{id}")
	@ResultMap("com.party.mapper.BlurbMapper.BlurbBaseMapped")
	Blurb selectIdBlurb(@Param("id")int id);
	
	//用商品id查询商品图片
	@Select("SELECT * FROM Img WHERE product_id = #{id}")
	@Results({
		@Result(property = "productid",  column = "product_id"),
		@Result(property = "address",  column = "img_address"),
	})
	List<Img> selectIdImg(@Param("id")int id);
	
	
	//删
	
	//id删除商品
	@Delete("DELETE FROM Product WHERE pid =#{id}")
	void delete(int id);
	
	//商品id删除商品详情
	@Delete("DELETE FROM Blurb WHERE product_id=#{id}")
	void deleteBlurb(int id);
	
	//商品id删除商品图片
	@Delete("DELETE FROM Img WHERE product_id=#{id}")
	void deleteImg(int id);
	
	
	
	//改
	
	//商品id更新商品
	@Update("UPDATE Product SET product_type=#{type},product_valid_time=#{validTime},product_city=#{city},product_description=#{description},product_title=#{title},product_value=#{value},product_add_time=#{addTime} WHERE pid =#{pid}")
	void updateProduct(Product pro);
	
	//商品id更新商品详情
	@Update("UPDATE Blurb SET blurb_address=#{address},blurb_number_people=#{numberPeople},blurb_number_bed=#{numberBed},blurb_number_room=#{numberRoom},blurb_supporting_facility=#{supportingFacility},blurb_label=#{blurbLabel},blurb_supplement=#{supplement},blurb_warm_prompt=#{warmPrompt},blurb_type=#{type} WHERE product_id =#{productId}")
	void updateBlurb(Blurb blu);
	
	
	
	
	//xml映射的sql
	
}
